﻿using HlELock.Core;
using HlELock.Domain.Entities;
using HlELock.Modules.Hotel.Agency.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Linq;
using System.Threading;
using System.Windows;

namespace HlELock.Modules.Hotel.Agency.ViewModels
{
    public class HotelMangeControlViewModel : BindableBase, INavigationAware
    {
        private bool _isEnabled = true;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                SetProperty(ref _isEnabled, value);
                if (value)
                {
                    Title = string.Empty;
                    Descr = string.Empty;
                }
            }
        }
        private static readonly object beginLockObject = new object();

        private int _number;
        public int Number
        {
            get { return _number; }
            set { SetProperty(ref _number, value); }
        }
        private int _qty1;
        /// <summary>
        /// 卡片加密数量
        /// </summary>
        public int Qty1
        {
            get { return _qty1; }
            set { SetProperty(ref _qty1, value); }
        }

        private int _qty2;
        /// <summary>
        /// 卡片注销数量
        /// </summary>
        public int Qty2
        {
            get { return _qty2; }
            set { SetProperty(ref _qty2, value); }
        }

        private int _qty3;
        /// <summary>
        /// 工程测试卡数量
        /// </summary>
        public int Qty3
        {
            get { return _qty3; }
            set { SetProperty(ref _qty3, value); }
        }

        private DelegateCommand _gongChengCommand;
        public DelegateCommand GongChengCommand =>
            _gongChengCommand ?? (_gongChengCommand = new DelegateCommand(ExecuteGongChengCommand));

        /// <summary>
        /// 工程卡
        /// </summary>
        void ExecuteGongChengCommand()
        {
            DisableWindow(true);
            Title = "工程测试卡";
            Descr = AppHelper.GetResourceString("QingJianKaPianFangDaoSheBeiShang");

            new Thread(() =>
            {
                lock (beginLockObject)
                {
                    var result = MyAppContext.Device.SendFactoryTestCard();
                    if (result.Result == UsbHidExtensions.SendCardResultType.成功)
                    {
                        Qty3++;
                        DisableWindow(true);
                        Thread.Sleep(200);
                        ExecuteGongChengCommand();

                    }
                    else
                    {
                        MessageBox.Show(result.Result == UsbHidExtensions.SendCardResultType.无卡或坏卡 ? "没有检测到卡片" : "发卡失败，请检查卡片");
                        DisableWindow(false);
                    }
                }
            }).Start();
        }

        private DelegateCommand _readCommand;
        public DelegateCommand ReadCommand =>
            _readCommand ?? (_readCommand = new DelegateCommand(ExecuteReadCommand));

        /// <summary>
        /// 读卡
        /// </summary>
        void ExecuteReadCommand()
        {
            DisableWindow(true);
            Title = "读卡";
            Descr = AppHelper.GetResourceString("正在读卡，请稍候");

            new Thread(() =>
            {
                lock (beginLockObject)
                {
                    var result = MyAppContext.Device.RedJmjCard();
                    string descr = string.Empty;
                    for (int i = 0; i < result.Length; i++)
                    {
                        descr += $"扇区{i+1}:{(result[i] ? "成功" : "错误")}";
                        if (i < result.Length - 1)
                        {
                            descr += "\r\n";
                        }
                    }

                    MessageBox.Show(descr);
                    DisableWindow(false);
                }
            }).Start();
        }

        private string _descr;
        public string Descr
        {
            get { return _descr; }
            set { SetProperty(ref _descr, value); }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private DelegateCommand _clearBorderCardCommand;
        public DelegateCommand ClearBorderCardCommand =>
            _clearBorderCardCommand ?? (_clearBorderCardCommand = new DelegateCommand(ExecuteClearBorderCardCommand));

        void ExecuteClearBorderCardCommand()
        {
            MyAppContext.Device.TestCard(cardCode =>
            {

            });
        }

        private DelegateCommand _clearCardCommand;
        public DelegateCommand ClearCardCommand =>
            _clearCardCommand ?? (_clearCardCommand = new DelegateCommand(() =>
            {
                DisableWindow(true);
                ExecuteClearCardCommand();
            }));



        /// <summary>
        /// 清除卡片
        /// </summary>
        void ExecuteClearCardCommand()
        {

            DisableWindow(true);
            Title = AppHelper.GetResourceString("QingKa");
            Descr = AppHelper.GetResourceString("QingJianKaPianFangDaoSheBeiShang");

            new Thread(() =>
            {
                lock (beginLockObject)
                {
                    var result = MyAppContext.Device.ClearCard();
                    if (result.Result == UsbHidExtensions.SendCardResultType.成功)
                    {
                        Qty2++;
                        DisableWindow(true);
                        Thread.Sleep(200);
                        ExecuteClearCardCommand();

                    }
                    else
                    {
                        MessageBox.Show("当前卡片没有加密或卡片异常，无法注销");
                        DisableWindow(false);
                    }
                }
            }).Start();


        }
        private DelegateCommand _beepCommand;
        public DelegateCommand BeepCommand =>
            _beepCommand ?? (_beepCommand = new DelegateCommand(ExecuteBeepCommand));

        /// <summary>
        /// 蜂鸣器
        /// </summary>
        void ExecuteBeepCommand()
        {
            MyAppContext.Device.Beep();
        }

        private DelegateCommand _sendCardCommand;
        public DelegateCommand SendCardCommand =>
            _sendCardCommand ?? (_sendCardCommand = new DelegateCommand(() =>
            {
                DisableWindow(true);
                ExecuteSendCardCommand();
            }));

        void DisableWindow(bool disable)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                IsEnabled = !disable;
            });
        }
        /// <summary>
        /// 加密卡片
        /// </summary>
        void ExecuteSendCardCommand()
        {

            Descr = AppHelper.GetResourceString("QingJianKaPianFangDaoSheBeiShang");
            Title = AppHelper.GetResourceString("JiaMiKaPian");

            new Thread(() =>
           {
               lock (beginLockObject)
               {
                   var result = MyAppContext.Device.SendJmjCard();
                   switch (result.Result)
                   {
                       case UsbHidExtensions.SendCardResultType.成功:
                           Application.Current.Dispatcher.Invoke(() =>
                           {
                               Qty1++;
                               DisableWindow(true);
                               Thread.Sleep(200);
                               ExecuteSendCardCommand();
                           });
                           break;
                       default:
                           Descr = AppHelper.GetResourceString("KaPianYiJiaMi");
                           MessageBox.Show(Descr);
                           DisableWindow(false);
                           break;
                   }
               }
           }).Start();
        }

        private DelegateCommand _testCardCommand;
        public DelegateCommand TestCardCommand =>
            _testCardCommand ?? (_testCardCommand = new DelegateCommand(ExecuteTestCardCommand));

        void ExecuteTestCardCommand()
        {
            var window = new TestCardWindow();
            window.ShowDialog();
        }

        private HotelInfo _hotelInfo;
        public HotelInfo HotelInfo
        {
            get { return _hotelInfo; }
            set { SetProperty(ref _hotelInfo, value); }
        }
        private readonly IRegionManager _regionManager;
        public HotelMangeControlViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            HotelInfo = navigationContext.Parameters.GetValue<HotelInfo>(nameof(HotelInfo));
            var number = MyAppContext.Device.GetNumber();
            Number = number;
            // MyAppContext.Device.WriteHotelCode(HotelInfo.HotelCode);
            //var hotelCode = MyAppContext.Device.ReadHotelCode();
            // if (hotelCode != HotelInfo.HotelCode)
            // {
            //     var info = AppHelper.GetResourceString("XieRuJiuDianBianMaShiBai");
            //     MessageBox.Show(info);
            //     EventBus.CloseDialog();
            // }
        }
    }
}
